// pages/organization/create.js
var app = getApp();
var util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    org_avatar_preview: '',
    org_name: '',
    org_intro: '',
    title_isnull: true,//判断组织名称是否为空
    code_isnull: true,//判断组织口令是否为空
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.getUserInfo(function (userInfo) {
      console.log(userInfo);
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
    wx.setNavigationBarTitle({
      title: "创建组织"
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //wx.setStorageSync('tempFilePaths', '');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //绑定数据
  //绑定组织名称是否为空
  bind_title: function (e) {
    console.log(e);
    if (e.detail.value == "") {
      this.setData({
        title_isnull: true,
      })
    } else {
      this.setData({
        title_isnull: false,
      })
    }
  },
  //绑定组织口令是否为空
  bind_code: function (e) {
    console.log(e);
    if (e.detail.value == "") {
      this.setData({
        code_isnull: true,
      })
    } else {
      this.setData({
        code_isnull: false,
      })
    }
  },


  //缓存组织头像
  add_org_avatar: function () {
    var that = this;
    console.log('Now Add Org Avatar..');
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths);
        //org_avatar_preview = tempFilePaths;
        wx.setStorageSync('tempFilePaths', tempFilePaths)
        that.setData({
          org_avatar_preview: tempFilePaths
        })
        return;
      }
    })
  },
  //绑定数据^
  //服务器

  //提交表单
  create_organization_request: function (e) {

    var that = this;
    if (this.data.title_isnull ||
      this.data.code_isnull) {//以免空信息

      if (that.data.org_avatar_preview == "") {//以免空头像

        wx.showModal({
          title: '信息不全',
          content: '请选择组织头像',
          showCancel: true,
          confirmText: '确定',
          cancelText: "返回",
          success: function (res) {
            if (res.confirm)
              that.add_org_avatar();
            else {
              wx.navigateBack({
                delta: 1
                ,
              })
            }
          },
          fail: function (res) { },
          complete: function (res) {
            return;
          },
        })
      } else {
        wx.showModal({
          title: '信息不全',
          content: '请填写完整',
          showCancel: false,
          confirmText: '确定',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { return; },
        })
      }
      return;
    } else {


      var value = e.detail.value;

      wx.showModal({//确认一下
        title: '创建组织',
        content: '确认提交后可以修改',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            var org_name = encodeURIComponent(value.org_name);
            var org_intro = value.org_intro;
            var tempFilePaths = wx.getStorageSync('tempFilePaths');
            var yuyue_session = wx.getStorageSync('yuyue_session');
            var token = wx.getStorageSync('token');
            var org_password = encodeURIComponent(value.org_password);
            console.log(tempFilePaths[0]);
            console.log(org_name);
            console.log(org_intro);

            var uploadTask = wx.uploadFile({//上传图片
              url: app.globalData.default_url + '/?action=api.v1.organization.create',
              filePath: tempFilePaths[0],
              name: 'file',
              formData: {
                'org_name': (org_name),
                'org_intro': (org_intro),
                'yuyue_session': yuyue_session,
                'password': (org_password),
                'token': token,
              },
              success: function (res) {

                var data = res.data
                data = JSON.parse(data);
                console.log(data);
                if (data.op == 'org_create') {
                  wx.showToast({
                    title: '创建组织成功',
                    duration: 1000,
                    mask: true,
                    success: function (res) { 
                      wx.navigateBack();},
                    fail: function (res) { },
                    complete: function (res) { },
                  })
                  console.log("oraganization_created");
                 
                } else {
                  wx.showModal({
                    title: '创建组织失败',
                    content: data.reason,
                    showCancel: true,
                    confirmText: '重试',
                    success: function (res) {
                      if (res.confirm) {
                        that.create_organization_request(e);
                      } else {
                        wx.navigateBack({
                          delta: 1,
                        })
                      }
                    },
                    fail: function (res) { },
                    complete: function (res) { return; },
                  })
                  console.log("oraganization_no_created");
                }
              },
              fail: function (data) {
                console.log("oraganization_created fail");
                console.log(data);
                util.neterror_Modal(function (e) {
                  that.create_organization_request(e);
                })
              },
              complete: function (data) {
                console.log("oraganization_created complete");
                console.log(data);

                return;
              }

            })
            if (uploadTask)
              uploadTask.onProgressUpdate((res) => {
                console.log('上传res', res)
                console.log('上传进度', res.progress)
                console.log('已经上传的数据长度', res.totalBytesSent)
                console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
              });

          } else if (res.cancel) {
            console.log('用户点击取消')
          }

        }
      })
    }
  },

  //服务器^
})